package com.voe;

import com.voe.callback.TraceCallback;
import com.voe.common.TraceLevel;

public class VoiceEngine {

	//[start] apis
	
	/** Creates a VoiceEngine object, which can then be used to acquire<br/>
     *  sub-APIs. Returns NULL on failure.
	 */
	public static VoiceEngine createInstance() {
		long p = Create();
		if(p != 0)
			return new VoiceEngine(p);
		return null;
	}
	
	/** Specifies the amount and type of trace information which will be<br/>
	 *  created by the VoiceEngine.
	 */
	public static int setTraceFilter(TraceLevel filter) {
		return SetTraceFilter(filter.getValue());
	}
	
	/** Sets the name of the trace file and enables non-encrypted trace messages.
	 */
	public static int setTraceFile(String fileNameUTF8, boolean addFileCounter) {
		return SetTraceFile(fileNameUTF8, addFileCounter);
	}
	
	/** Sets the name of the trace file and enables non-encrypted trace messages.
	 */
	public static int setTraceFile(String fileNameUTF8) {
		return setTraceFile(fileNameUTF8, false);
	}
	
	/** Installs the TraceCallback implementation to ensure that the user<br/>
	 *  receives callbacks for generated trace messages.
	 */
	public static int setTraceCallback(TraceCallback callback) {
		return SetTraceCallback(callback);
	}

	/** Deletes a created VoiceEngine object and releases the utilized resources.<br/>
     *  Note that if there are outstanding references held via other interfaces,<br/>
     *  the voice engine instance will not actually be deleted until those<br/>
     *  references have been released.
     */
	public void release() {
		if (_p != 0) {
			Delete(_p);
		}
	}
	
	protected VoiceEngine(long p) {
		_p = p;
	}
	
	@Override
	protected void finalize() {
		release();
	}
	
	protected long _p;
	
	//[end]
	
	
	
	
	
	
	//[start] jni api
	
	// Creates a VoiceEngine object, which can then be used to acquire
    // sub-APIs. Returns NULL on failure.
	private static native long Create();

	// Deletes a created VoiceEngine object and releases the utilized resources.
    // Note that if there are outstanding references held via other interfaces,
    // the voice engine instance will not actually be deleted until those
    // references have been released.
	private static native boolean Delete(long p);
	
    // Specifies the amount and type of trace information which will be
    // created by the VoiceEngine.
	private static native int SetTraceFilter(int filter);

    // Sets the name of the trace file and enables non-encrypted trace messages.
	private static native int SetTraceFile(String fileNameUTF8, boolean addFileCounter);

    // Installs the TraceCallback implementation to ensure that the user
    // receives callbacks for generated trace messages.
	private static native int SetTraceCallback(TraceCallback callback);

	//[end]

	static {
		try {
			System.loadLibrary("voice_engine_jni");
		} catch(UnsatisfiedLinkError e) {
			e.printStackTrace();
		}
	}
}
